package br.com.senac.digipao.servlet;

import br.com.senac.digipao.bean.ContasPagar;
import br.com.senac.digipao.bean.Usuario;
import br.com.senac.digipao.dao.ContasDao;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "alteraCadastroContasPagarServlet", urlPatterns = {"/alteraCadastroContasPagar"})
public class AlteraCadastroContasPagarServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");

        String operacao = request.getParameter("operacao");

        if (operacao.equals("Alterar")) {
            
             ContasPagar contasPagarParaAlterar = (ContasPagar) request.getSession(true).getAttribute("ContasPagarParaAlterar");
             
            // pegando os parâmetros do request
            int idContasPagar = contasPagarParaAlterar.getIdContasPagar();
            String descricaoConta = request.getParameter("descricaoConta");
            String dataCadastro = request.getParameter("dataCadastro");
            String dataVencimento = request.getParameter("dataVencimento");
            String dataPagamento = request.getParameter("dataPagamento");

            // monta um objeto contasPagar
            ContasPagar contasPagar = new ContasPagar();
            contasPagar.setIdContasPagar(idContasPagar);
            contasPagar.setDescricaoConta(descricaoConta);
            contasPagar.setDataCadastro(dataCadastro);
            contasPagar.setDataVencimento(dataVencimento);
            contasPagar.setDataPagamento(dataPagamento);

            // salva o cadastro do contasPagar
            try {
                ContasDao dao = new ContasDao();
                dao.alteraContasPagar(contasPagar);

            } catch (SQLException ex) {
                Logger.getLogger(CadastroContaPagarServlet.class.getName()).log(Level.SEVERE, null, ex);
            } 
            
            request.getSession().removeAttribute("ContasPagarParaAlterar");
            request.getSession().removeAttribute("ListaContasPagar");
            RequestDispatcher rd = request.getRequestDispatcher("/media/jsp/alteradoComSucesso.jsp");
             rd.forward(request, response);
        } else if (operacao.equals("Cancelar")) {
            RequestDispatcher rd = request.getRequestDispatcher("/media/jsp/menuPesquisar.jsp");
            rd.forward(request, response);
        }

    }

}
